package com.mitake.nick.example.socket;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerSocket serverSocket;
		Socket socket;
		String str;
		InputStream is;
		OutputStream os;
		DataInputStream dis;
		PrintStream ps;
		try {
			serverSocket = new ServerSocket(100);
			socket = serverSocket.accept();
			is = socket.getInputStream();
			os = socket.getOutputStream();
			dis = new DataInputStream(is);
			ps = new PrintStream(os);
			DataInputStream in = new DataInputStream(System.in);
			
			while(true){
				System.out.println("");
				System.out.println("please wait client's message...");
				System.out.println("");
				str = dis.readLine();
				System.out.println("client said"+ str);
				
				if("bye".equals(str.trim())){
					System.out.print("you said:");
					str = in.readLine();
					ps.println(str);
					if("bye".equals(str.trim())){
						break;
					}
				}
			}
			
			dis.close();
			ps.close();
			is.close();
			os.close();
			socket.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
